<?php
class resource
{
	public $table;
	public $pages;
	public $db;
	public $error;
	public $current_user;
	public $tree;
	public $sites; 
	public $view_type;
	
	function resource()
	{
		global $db,$current_user;
        $this->db = &$db;
        $this->table = DB_PRE.'resource';
        $this->current_user = $current_user;
        $this->sites = array();
        $this->view_type = array();
	}
	
	/**
	 * 添加资源，sql必须有title,id,pid字段
	 *
	 * @param char $view_type
	 * @param char $name
	 * @param char $sql
	 * @return int or false
	 */
	function add_resource($view_type,$name,$sql, $is_site)
	{
		$time = time();
		$params = array(
			'view_type'	=>	$view_type,
			'is_sites'	=>	$is_site,
			'res_name'	=>	$name,
			'res_sql'	=>	$sql,
			'listorder'	=>	0,
			'inputtime'	=>	$time,
			'updatetime'=>	$time,
			'username'	=>	$this->current_user['username']
		);
		$res = $this->db->insert($this->table,$params);
		return $res;
	}
	
	/**
	 * 修改资源配置信息
	 *
	 * @param int $resid
	 * @param varchar $view_type
	 * @param varchar $name
	 * @param varchar $sql
	 * @return bool
	 */
	function edit_resource($resid,$view_type,$name,$sql, $is_site)
	{
		$time = time();
		$params = array(
			'view_type'	=>	$view_type,
			'is_sites'	=>	$is_site,
			'res_name'	=>	$name,
			'res_sql'	=>	$sql,
			'updatetime'=>	$time,
			'username'	=>	$this->current_user['username']
		);
		$condition = array('resid'=>$resid);
		return $this->db->simple_update($this->table,$params,$condition);
	}
	
	/**
	 * 修改资源配置排序
	 *
	 * @param int $resid
	 * @param int $listorder
	 * @return bool
	 */
	function edit_listorder($resid,$listorder)
	{
		$time = time();
		$params = array(
			'listorder'	=>	$listorder,
			'updatetime'=>	$time,
			'username'	=>	$this->current_user['username']
		);
		$condition = array('resid'=>$resid);
		return $this->db->simple_update($this->table,$params,$condition);
	}
	
	/**
	 * 删除资源配置
	 *
	 * @param int $resid
	 * @return bool
	 */
	function delete($resid)
	{
        return $this->db->fake_delete($this->table, array('resid'=>$resid));
	}
	
	/**
	 * 获得所有资源配置列表
	 *
	 * @return array
	 */
	function get_res()
	{
		$list = $this->db->query('select * from '.$this->table.' where status='.STATUS_NORMAL.' order by listorder desc,inputtime desc');
		return $list;
	}
	
	/**
	 * 根据id获得资源配置
	 *
	 * @param int $resid
	 * @return array
	 */
	function get_res_by_id($resid)
	{
		$this->db->prepare('select *  from '.$this->table.' where status=? and resid=?');
		$this->db->set_int(STATUS_NORMAL);
		$this->db->set_string($resid);
		$res = $this->db->execute(true);
		return $res;
	}
	
	/**
	 * 根据
	 *
	 * @param unknown_type $view_type
	 * @return unknown
	 */
	function get_res_by_view_type($view_type)
	{
		$this->db->prepare('select *  from '.$this->table.' where status=? and view_type=?');
		$this->db->set_int(STATUS_NORMAL);
		$this->db->set_string($view_type);
		$res = $this->db->execute(true);
		return $res;
	}
	
	/**
	 * 查看资源配置编码是否存在
	 *
	 * @param string $view_type
	 * @return bool
	 */
	function check_view_type($view_type)
	{
		$this->db->prepare('select count(*) as nums from '.$this->table.' where status=? and view_type=?');
		$this->db->set_int(STATUS_NORMAL);
		$this->db->set_string($view_type);
		$res = $this->db->execute(true);
		if(intval($res['nums']) > 0)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	/**
	 * 获得资源列表
	 *
	 * @param string $view_type
	 * @param array $views
	 * @return sring
	 */
	function get_resource($view_type,$views)
	{
		$sites = site_list();
		$this->sites = $sites;
		$this->view_type = $this->get_res_by_view_type($view_type);
		$res_conf = $this->get_res_by_view_type($view_type);
		$res = $this->db->query($res_conf['res_sql'].' order by siteid desc, listorder desc');
		$list = array();
		foreach ($res as $v)
		{
			$list[$v['pid']][] = $v;
		}
		$final = array(
			'string'=>'',
			'prev'=>'',
			'tid'=>'t'
		);
		$prev = '';
		$this->make_tree(0,$list,$final,$views);
		return $final['string'];
	}
	
	/**
	 * 生成资源选择树
	 *
	 * @param int $pid
	 * @param array $array
	 * @param array $res
	 * @param array $views
	 * @return unknown
	 */
	function make_tree($pid,$array,&$res,$views)
	{
		$prev_string = '&nbsp;&nbsp;&nbsp;&nbsp;';
		if(isset($array[$pid]) && is_array($array[$pid]) )
		{
			$size = sizeof($array[$pid]);
			$i=1;
			$flag = true;
			$sites_flag = array();
			foreach ($array[$pid] as $k=>$v)
			{
				if(!in_array($v['siteid'], $sites_flag))
				{
					$sites_flag[] = $v['siteid'];
				}
			}
			$start_sistes = 'no_sites';
			foreach ($array[$pid] as $k=>$v)
			{
				$tmp = array(
					'string'=>'',
					'prev'=>$res['prev'].$prev_string,
					'tid'=>$res['tid'].'_'.$v['id']
				);
				
				$r = $this->make_tree($v['id'],$array,$tmp,$views);
				if(in_array($v['id'],$views))
				{
					$checked = 'checked';
				}
				else 
				{
					$checked = '';
				}
				if($r==false)
				{
					
					if($i==1)
					{
						$pos = $res['prev'].'<input type="checkbox" '.$checked.' onclick="next_select(this)" id="'.$res['tid'].'_'.$v['id'].'" name=views[] value="'.$v['id'].'">&nbsp;'.$v['title'].'__edufe_nbsp__';
					}
					else 
					{
						$nbsp_flag=false;
						foreach ($array[$pid] as $chk)
						{
							if(isset($array[$chk['id']]))
							{
								$nbsp_flag = true;
							}
						}
						$pos = '<input type="checkbox" '.$checked.' onclick="next_select(this)" id="'.$res['tid'].'_'.$v['id'].'" name=views[] value="'.$v['id'].'">&nbsp;'.$v['title'].'__edufe_nbsp__';
						if($nbsp_flag==true)
						{
							$pos = $res['prev'].$pos;
						}
					}
					if($i%7==0 || $size==1 || $i==$size)
					{
						if($i==$size || $size==1)
						{
							$pos .= '__edufe_line_br__';
						}
						else 
						{
							$pos .= '__edufe_line_br__'.$res['prev'];
						}
					}
				}
				else 
				{
					$flag = false;
					$pos = $res['prev'].'<input type="checkbox" '.$checked.' onclick="next_select(this)" id="'.$res['tid'].'_'.$v['id'].'" name=views[] value="'.$v['id'].'">&nbsp;<strong>'.$v['title'].'</strong>__edufe_br__';
				}
				if($v['siteid'] != $start_sistes && $this->view_type['is_sites']==1 && sizeof($sites_flag) > 1)
				{
					$site_name = (empty($v['siteid']) || $v['siteid']=='main') ? '主站' : (isset($this->sites[$v['siteid']]) ? $this->sites[$v['siteid']] : '未知站点');
					$pos = $res['prev'].'<strong>' . $site_name . '</strong>__edufe_br__' . $pos;
					if($i > 1)
					{
						$pos = '__edufe_br__' . $pos;
					}
					$start_sistes = $v['siteid'];
				}
				$res['string'] .= $pos;
				$res['string'] .= $tmp['string'];
				$i++;
			}
			$nbsp_replace = $flag===false ? "<br/>" : "&nbsp;&nbsp;";
			$line_br_replace = $flag===false ? "" : "<br/>";
			$res['string'] = str_replace(array('__edufe_nbsp__','__edufe_br__','__edufe_line_br__'),array($nbsp_replace,'<br/>',$line_br_replace),$res['string']);
			return true;
		}
		else 
		{
			return false;
		}
	}
}
?>